home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / utils.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  2KB  |  66 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: utils.py 647 2006-08-26 18:27:39Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import sys
  7. if sys.hexversion < 33751040:
  8.     raise ImportError, 'Python 2.3 or newer is required'
  9.  
  10. import time
  11. import datetime
  12.  
  13. def to_utf8(s):
  14.     if s is None:
  15.         return None
  16.     elif type(s) is unicode:
  17.         return s.encode('utf-8')
  18.     elif type(s) is str:
  19.         return s
  20.     else:
  21.         return unicode(s).encode('utf-8')
  22.  
  23.  
  24. def from_utf8(s):
  25.     if s is None:
  26.         return None
  27.     elif type(s) is unicode:
  28.         return s
  29.     elif type(s) is str:
  30.         return unicode(s, 'utf-8')
  31.     else:
  32.         return unicode(s)
  33.  
  34. minute = datetime.timedelta(minutes = 1)
  35. nulldelta = datetime.timedelta()
  36.  
  37. def datetime_utc_to_local(utc):
  38.     ts = time.time()
  39.     cur = datetime.datetime.fromtimestamp(ts)
  40.     cur_utc = datetime.datetime.utcfromtimestamp(ts)
  41.     offset = cur - cur_utc
  42.     t = utc
  43.     d = datetime.timedelta(hours = 2)
  44.     while d > minute:
  45.         local = t + offset
  46.         tm = local.timetuple()
  47.         tm = tm[0:8] + (0,)
  48.         ts = time.mktime(tm)
  49.         u = datetime.datetime.utcfromtimestamp(ts)
  50.         diff = u - utc
  51.         if diff < minute and diff > -minute:
  52.             break
  53.         
  54.         if diff > nulldelta:
  55.             offset -= d
  56.         else:
  57.             offset += d
  58.         d /= 2
  59.     return local
  60.  
  61.  
  62. def datetime_local_to_utc(local):
  63.     ts = time.mktime(local.timetuple())
  64.     return datetime.datetime.utcfromtimestamp(ts)
  65.  
  66.